<!DOCTYPE html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>字典表单</title>
  <th:block th:include="common/public::layui"></th:block>
</head>
<body>
<form class="layui-form layui-form-pane" style="margin: 15px" lay-filter="form">
  <div class="layui-form-item">
    <label class="layui-form-label">编码</label>
    <div class="layui-input-block">
      <input type="hidden" name="id">
      <input type="text" name="code" placeholder="请输入编码" autocomplete="off" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">键名</label>
    <div class="layui-input-block">
      <input type="text" name="dicKey" placeholder="请输入键名" autocomplete="off" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">键值</label>
    <div class="layui-input-block">
      <input type="text" name="dicValue" placeholder="请输入键值" autocomplete="off" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item" style="text-align: center">
    <div class="layui-inline">
      <button class="layui-btn" lay-submit lay-filter="*">提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
</body>
<script th:inline="javascript">
  layui.use(['form', 'jquery'], function () {
    var id = [[${id}]];
    var form = layui.form;
    var $ = layui.$;
    if (id) {
      $.get(prefixUrl + '/dic/get/' + id, {}, function (res) {
        form.val('form', res);
      }, 'json');
    }
    form.on('submit(*)', function (data) {
      if (id) {
        $.post(prefixUrl + '/dic/update', data.field, function (res) {
          if (res.status == 200) {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
            parent.layer.msg(res.msg, {icon: 1});
            parent.table.reload('table');
          } else {
            parent.layer.alert(res.msg, {icon: 2, anim: 6})
          }
        }, 'json');
      } else {
        $.post(prefixUrl + '/dic/add', data.field, function (res) {
          if (res.status == 200) {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
            parent.layer.msg(res.msg, {icon: 1});
            parent.table.reload('table');
          } else {
            parent.layer.alert(res.msg, {icon: 2, anim: 6})
          }
        }, 'json');
      }
      return false;
    })
  })
</script>
</html>